package com.st.mall.util.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/AuthCodeController")
public class AuthCodeController {

	private static char[] chs= { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
			'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 
			'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j',
			'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
			'x', 't', 'z', '2', '3', '4', '5', '6', '7', '8', '9'};
	private static Random r = new Random();
	public static String name = "authcode";
	
	@RequestMapping("/auth")
	public void auth(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		BufferedImage image = new BufferedImage(100, 43, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(new Color(200, 200, 255));
		g.fillRect(0, 0, 100, 43);
		g.setFont(new Font("隶书", Font.BOLD, 30));	
		StringBuffer sf = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			int index = r.nextInt(chs.length);
			sf.append(chs[index]);
			g.setColor(new Color(r.nextInt(200), r.nextInt(200), r.nextInt(255)));
			g.drawString(chs[index] + "", 25 * i, 30);
		}
		req.getSession().setAttribute(name, sf.toString());//保存验证码信息
		ImageIO.write(image,"jpg", resp.getOutputStream());
	}
}
